#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_AHTX0.h>
#include "aht20.h"
#include "config.h"

Adafruit_AHTX0 aht;

void aht20_init()
{
  // note:由于aht20库使用的是默认的wire，这里直接覆写掉了Wire库的引脚定义
  Wire.setPins(AHT20_I2C_SDA, AHT20_I2C_SCL);

  if (!aht.begin())
  {
    Serial.println("Could not find AHT? Check wiring");
    return;
  }
  Serial.println("AHT20 Found!");
}

sensors_event_t humidity, temp;
int aht20_get_n = 0;
aht20_result_t aht20_get_result()
{
  if (aht20_get_n % 10 == 0) // 10倍于 INTERVAL_ANIM_MULTIPLIER_AHT20 * sys_tick_ms = 100ms
    aht.getEvent(&humidity, &temp);

  aht20_get_n++;
#if 1
  Serial.print("Temperature: ");
  Serial.print(temp.temperature);
  Serial.println(" degrees C");
  Serial.print("Humidity: ");
  Serial.print(humidity.relative_humidity);
  Serial.println("% rH");
#endif
  aht20_result_t res = {temp.temperature, humidity.relative_humidity};
  return res;
}